//
//  ConpickUpViewController.m
//  Shengzhuli
//
//  Created by subook on 2017/8/14.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConpickUpViewController.h"
#import "PickCommentCell.h"
#import "ConPublishViewController.h"
#import "PickCommentView.h"
#import "PickUpNetwork.h"
#import "SZLAccountHelper.h"
#import "TalkTableCell.h"
#import "PickUpModel.h"
#import "PickUpCommentModel.h"
#import <MJRefresh.h>
#import "MBProgressHUD+MJ.h"
#import "AppDelegate.h"
#import "IQKeyboardManager.h"
#import "GGIndicatorHud.h"


@interface ConpickUpViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    UITableView *myTableView;
    UIImageView *headerImageView;
    UILabel *zanGotLabel;
    UILabel *huozanLabel;
    UILabel *pingGotLabel;
    UILabel *huopingLabel;
    UILabel *postedLabel;
    UILabel *fatieLabel;
    
    UIView *lineView;
    UIButton *selectedBtn;
    UIView *sectionHeaderView;
    NSMutableArray *dataArray;
    NSMutableArray *moreCommentsArray;

    int pageNum;
    NSString *talkId;
    UITextField *print;//评论输入框
    UIButton *sendButton;//提交按钮
    UIView *keyboardTopView;//输入框view
    
    NSIndexPath *commentBtnIndexPath;
    
    UIView *imageBottomView;
    UIImageView *bigImageView;
    UITapGestureRecognizer *keyBoardHidetap;
}

@end

@implementation ConpickUpViewController

- (CGSize)AutoSizeWithStr:(NSString *)str textfont:(UIFont *)textfont size:(CGSize)size {
    return [str boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
            NSStringDrawingUsesLineFragmentOrigin |
            NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:textfont} context:nil].size;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (sectionHeaderView) {
        return sectionHeaderView;

    }
    sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 50)];
    sectionHeaderView.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftButton.frame = (CGRectMake(0, 0, ScreenW/2 , 50));
    [leftButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [leftButton setTitle:@"主贴" forState:(UIControlStateNormal)];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0, ScreenW/2-80, 0, 40)];
    [leftButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    selectedBtn = leftButton;
    leftButton.tag = 100;
    [sectionHeaderView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightButton.frame = (CGRectMake(ScreenW/2, 0, ScreenW/2 , 50));
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitle:@"参与" forState:(UIControlStateNormal)];
    [rightButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 40, 0, ScreenW/2-80)];
    [rightButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    rightButton.tag = 101;
    [sectionHeaderView addSubview:rightButton];
    
    UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 49.5, ScreenW, 0.5)];
    spaceView.backgroundColor = RGBA(230, 230, 230, 1);
    [sectionHeaderView addSubview:spaceView];
    
    lineView = [[UIView alloc] initWithFrame:CGRectMake(ScreenW/2-76, 39, 32, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:68/255.0 green:192/255.0 blue:250/255.0 alpha:1];
    [sectionHeaderView addSubview:lineView];
    
    return sectionHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PickUpModel *model = dataArray[indexPath.row];

    CGFloat titleHeight = [self AutoSizeWithStr:model.ruleTitle textfont:[UIFont systemFontOfSize:16] size:CGSizeMake(ScreenW-30, MAXFLOAT)].height;
    
    CGFloat contentHeight = [self AutoSizeWithStr:model.ruleContent textfont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenW-30, MAXFLOAT)].height;
    
    if (model.pictureUrlList.count>0) {
        CGFloat imageWidth = (ScreenW-30-5*2)/3;
        
        if (model.ruleCommentsList.count > 0) {
            CGFloat labelBottom = 0;
            for (int i=0; i<model.ruleCommentsList.count; i++) {
                PickUpCommentModel *commentModel = model.ruleCommentsList[i];
                
                CGFloat commentHeight = [self AutoSizeWithStr:[NSString stringWithFormat:@"%@：%@",commentModel.nickName,commentModel.ruleCommentContent] textfont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenW-50, MAXFLOAT)].height;
                
                labelBottom = labelBottom+5+commentHeight;
                
            }
            
            if ([model.commentNumberStatus boolValue] && model.ruleCommentsList.count < 4) {
                
                return 20+ labelBottom+5+10 +15+35+10 +titleHeight +10+contentHeight+10+imageWidth+10 +20+15+0.5;

            }
            
            return labelBottom+5+10 +15+35+10 +titleHeight +10+contentHeight+10+imageWidth+10 +20+15+0.5;
        }

        return 15+35+10 +titleHeight +10+contentHeight+10+imageWidth+10 +20+10+0.5;
    }
    if (model.ruleCommentsList.count > 0) {
        CGFloat labelBottom = 0;
        for (int i=0; i<model.ruleCommentsList.count; i++) {
            PickUpCommentModel *commentModel = model.ruleCommentsList[i];
            
            CGFloat commentHeight = [self AutoSizeWithStr:[NSString stringWithFormat:@"%@：%@",commentModel.nickName,commentModel.ruleCommentContent] textfont:[UIFont systemFontOfSize:14] size:CGSizeMake(ScreenW-50, MAXFLOAT)].height;
            
            labelBottom = labelBottom+5+commentHeight;
        }
        
        if ([model.commentNumberStatus boolValue] && model.ruleCommentsList.count < 4) {
            
            return 20+ labelBottom+5+10 + 15+35+10 +titleHeight +10+contentHeight+10 +20+15+0.5;
        }
        
        return labelBottom+5+10 + 15+35+10 +titleHeight +10+contentHeight+10 +20+15+0.5;

    }
    return 15+35+10 +titleHeight +10+contentHeight+10 +20+10+0.5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TalkTableCell *talkCell = [tableView dequeueReusableCellWithIdentifier:@"TalkTableCell"];
    talkCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    PickUpModel *model = dataArray[indexPath.row];
    [talkCell updateCellWithModel:model];
    
    __weak ConpickUpViewController *myself = self;
    [talkCell setLookMoreBlock:^(){
        
        [myself loadMoreCommentsListDataWithId:model.id indexPath:indexPath];
    }];
    
    [talkCell setImageTapBlock:^(UIImage *image){
        imageBottomView.hidden = NO;
        bigImageView.image = image;
        
        if (image.size.width/image.size.height > ScreenW/ScreenH) {
            bigImageView.frame = CGRectMake(0, (ScreenH-ScreenW*image.size.height/image.size.width)/2, ScreenW, ScreenW*image.size.height/image.size.width);
        } else {
            bigImageView.frame = CGRectMake((ScreenW-ScreenH*image.size.width/image.size.height)/2, 0, ScreenH*image.size.width/image.size.height, ScreenH);

        }
        
    }];
    
    __weak TalkTableCell *weakTalkCell = talkCell;

    talkCell.zanButtonBlock = ^(){
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"token"] = [SZLAccountHelper shareInstance].token;
        param[@"id"] = model.id;
        [PickUpNetwork getZanWithParame:param response:^(BOOL success, NSString *msg) {
            if (success) {
                weakTalkCell.zanButton.selected = !weakTalkCell.zanButton.selected;
                if (weakTalkCell.zanButton.selected == YES) {
                    int a= [weakTalkCell.zanNumLabel.text intValue]+1;
                    weakTalkCell.zanNumLabel.text = [NSString stringWithFormat:@"%d",a];
                    [MBProgressHUD showSuccess:@"点赞成功!"];
                    
                    model.isNotPraise = @"1";
                    model.praise = [NSString stringWithFormat:@"%d",a];
                    [dataArray replaceObjectAtIndex:indexPath.row withObject:model];
                    
                }else {
                    int b= [weakTalkCell.zanNumLabel.text intValue]-1;
                    weakTalkCell.zanNumLabel.text = [NSString stringWithFormat:@"%d",b];
                    [MBProgressHUD showSuccess:@"点赞取消!"];
                    model.isNotPraise = @"0";
                    model.praise = [NSString stringWithFormat:@"%d",b];
                    [dataArray replaceObjectAtIndex:indexPath.row withObject:model];
                }
            }else {
                [MBProgressHUD showError:msg];
            }
        }];
        
    };
    //点击开始评论
    talkCell.pingButtonBlock = ^(){
        
        [print becomeFirstResponder];
        commentBtnIndexPath = indexPath;
        //        CGRect frame = keyboardTopView.frame;
        //        frame.origin.y= ScreenH-500;
        //        keyboardTopView.frame = frame;
        talkId = model.id;
    };

    
    return talkCell;
}

- (void)createCustomTextFiled {
    
    keyboardTopView = [[UIView alloc] initWithFrame:(CGRectMake(0, ScreenH, ScreenW, 50))];
    keyboardTopView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:keyboardTopView];

    
    print = [[UITextField alloc] initWithFrame:(CGRectMake(20, 10, ScreenW-100, 30))];
    print.placeholder = @"请输入评论内容";
    print.delegate = self;
    sendButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sendButton.frame = CGRectMake(ScreenW-70, 10, 50, 30);
    [sendButton setTitle:@"提交" forState:(UIControlStateNormal)];
    [sendButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [sendButton addTarget:self action:@selector(sendButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [keyboardTopView addSubview:print];
    [keyboardTopView addSubview:sendButton];
}
//评论发表按钮
- (void)sendButtonClick:(UIButton *)btn {
    
    [print resignFirstResponder];
    if (print.text.length>0) {
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"token"] = [SZLAccountHelper shareInstance].token;
        param[@"id"] = talkId;
        param[@"ruleCommentContent"] = print.text;
        
        __weak ConpickUpViewController *myself = self;
        [PickUpNetwork getSendCommentWithParame:param response:^(BOOL success, NSString *msg, NSMutableArray *arr) {
            if (success) {
                
                [MBProgressHUD showSuccess:@"评论成功!"];
                
                TalkTableCell *cell = [myTableView cellForRowAtIndexPath:commentBtnIndexPath];
                
                int a= [cell.pingNumLabel.text intValue]+1;
                cell.pingNumLabel.text = [NSString stringWithFormat:@"%d",a];
                
                PickUpModel *model = dataArray[commentBtnIndexPath.row];
                
                model.commentsNumber = [NSString stringWithFormat:@"%d",a];

                ///////////////////////
                PickUpCommentModel *commentModel = [[PickUpCommentModel alloc] init];
                commentModel.nickName = myself.nickName;
                commentModel.ruleCommentContent = print.text;
                //如果未超过三条
                if (![model.commentNumberStatus boolValue] || cell.lookMoreBtn.hidden == YES) {
                    [model.ruleCommentsList addObject:commentModel];
                    [dataArray replaceObjectAtIndex:commentBtnIndexPath.row withObject:model];
                    NSLog(@"没有超过三条");
                    [myTableView reloadRowsAtIndexPaths:@[commentBtnIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                }else {
                    
                    [self loadMoreCommentsListDataWithId:model.id indexPath:commentBtnIndexPath];
                }                ////////////////////////
                print.text = @"";

            }else {
                [MBProgressHUD showError:@"评论失败!"];
                print.text = @"";
            }
        }];
    }else {
        [MBProgressHUD showError:@"评论不能为空!"];
    }
    
    
}

- (void)loadMoreCommentsListDataWithId:(NSString *)idStr indexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param [@"id"] = idStr;
    [PickUpNetwork getMoreCommentWithParame:param response:^(BOOL success, NSString *msg, NSMutableArray *arr) {
        if (success) {
            PickUpModel *model = dataArray[indexPath.row];

            [moreCommentsArray removeAllObjects];
            [moreCommentsArray addObjectsFromArray:model.ruleCommentsList];

            for (NSMutableDictionary *commentDict in arr) {
                
                PickUpCommentModel *commentModel = [[PickUpCommentModel alloc] init];
                [commentModel setModelWithDict:commentDict];
                [moreCommentsArray addObject:commentModel];
            }
            
            model.ruleCommentsList = [NSMutableArray arrayWithArray:moreCommentsArray];
            [dataArray replaceObjectAtIndex:indexPath.row withObject:model];
            
            [myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }];
}

- (void)selectedButtonClick:(UIButton *)button {

    if (button.tag == selectedBtn.tag) {
        return;
    }
    selectedBtn = button;
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = lineView.frame;
        frame.origin.x = ScreenW/2-76 +(selectedBtn.tag-100)*(44+76);
        lineView.frame = frame;
    }];
    pageNum = 1;
    [dataArray removeAllObjects];
    [self loadTalkDataWithType:[NSString stringWithFormat:@"%ld",selectedBtn.tag-100]];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"捡漏专区";
    self.view.backgroundColor = [UIColor whiteColor];
    dataArray = [NSMutableArray array];
    moreCommentsArray = [NSMutableArray array];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   
    
    pageNum = 1;
    
    [self configNaviItem];
    [self configTableView];
    [self configHeaderView];
    [self loadTalkDataWithType:@"0"];
    [self loadData];
    [self createCustomTextFiled];
    
    imageBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    imageBottomView.userInteractionEnabled = YES;
    [[UIApplication sharedApplication].delegate.window addSubview:imageBottomView];
    imageBottomView.backgroundColor = [UIColor blackColor];

    bigImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [imageBottomView addSubview:bigImageView];
    imageBottomView.hidden = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageBottomViewTap)];
    [imageBottomView addGestureRecognizer:tap];

}

- (void)imageBottomViewTap {
    imageBottomView.hidden = YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}
- (void)keyboardWillShow:(NSNotification *)aNotification{
    

    NSDictionary* info = [aNotification userInfo];
    
    CGRect kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];//得
    
    CGRect frame = keyboardTopView.frame;
    frame.origin.y = kbSize.origin.y-110;
    keyboardTopView.frame = frame;
    
    
    CGPoint point =  myTableView.contentOffset;
    point.y = point.y+kbSize.size.height/2;
    myTableView.contentOffset = point;
    
    keyBoardHidetap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHide)];
    [myTableView addGestureRecognizer:keyBoardHidetap];
    
}

- (void)tapHide {
    [print resignFirstResponder];
    NSLog(@"lalallalal");
}

- (void)keyboardWillHide:(NSNotification *)aNotification {
    
    

    [myTableView removeGestureRecognizer:keyBoardHidetap];
    NSDictionary* info = [aNotification userInfo];
    
    CGRect kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardTopView.frame = CGRectMake(0, ScreenH-64, ScreenW, 50);
    
    CGPoint point =  myTableView.contentOffset;
    point.y = point.y-kbSize.size.height/2;
    myTableView.contentOffset = point;
}
- (void)viewWillAppear:(BOOL)animated {
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;

    [[IQKeyboardManager sharedManager] setEnable:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}

- (void)loadTalkDataWithType:(NSString *)type {
    [GGIndicatorHud showIndicatorInView:self.view];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param [@"token"]= [SZLAccountHelper shareInstance].token;
    param [@"pageNo"] = [NSString stringWithFormat:@"%d",pageNum];
    param [@"type"] = type;
    [PickUpNetwork getMyPickWithParame:param response:^(BOOL success, NSString *msg, NSMutableArray *arr) {
        [GGIndicatorHud hideIndicatorHudInView:self.view];
        if (success) {
            
            [myTableView.mj_header endRefreshing];
            [myTableView.mj_footer endRefreshing];
            
            if (arr.count > 0) {
                pageNum++;

            } else {

                [myTableView.mj_footer endRefreshingWithNoMoreData];
            }

            for (NSMutableDictionary *dict in arr) {
                PickUpModel *model = [[PickUpModel alloc] init];
                [model setModelWithDict:dict];
                
                NSMutableArray *commentArray = [NSMutableArray array];
                for (NSMutableDictionary *commentDict in model.ruleCommentsList) {
                    PickUpCommentModel *commentModel = [[PickUpCommentModel alloc] init];
                    [commentModel setModelWithDict:commentDict];
                    [commentArray addObject:commentModel];
                    
                }
                model.ruleCommentsList = [NSMutableArray arrayWithArray:commentArray];
                
                [dataArray addObject:model];
                
            }

            [myTableView reloadData];
        }
    }];
}
- (void)loadData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param [@"token"]= [SZLAccountHelper shareInstance].token;
    
    [PickUpNetwork getThreeLabelNumWithParame:param response:^(BOOL success, NSString *msg,NSMutableDictionary *dict) {
        
        if (success) {
            zanGotLabel.text = [dict[@"praises"] stringValue];
            pingGotLabel.text = [dict[@"commentsNumber"] stringValue];
            postedLabel.text = [dict[@"posts"] stringValue];
            
        }
    }];
    
    
}

//点击发表按钮
- (void)gotoPublishButtonClicked {

    ConPublishViewController *cpvc = [[ConPublishViewController alloc] init];
    [self.navigationController pushViewController:cpvc animated:YES];
    
}

- (void)configHeaderView {
    
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenW/2+60+10)];
    
    headerImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenW/2))];
    headerImageView.image = GetImage(@"headimage2");
    [tableHeaderView addSubview:headerImageView];
    
    UIView *numView = [[UIView alloc] initWithFrame:(CGRectMake(0, headerImageView.bottom, ScreenW, 60))];
    [tableHeaderView addSubview:numView];
    
    UIView *spaceView = [[UIView alloc] initWithFrame:CGRectMake(0, numView.bottom, ScreenW, 10)];
    spaceView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [tableHeaderView addSubview:spaceView];

    
    //获赞
    zanGotLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-100, 15, 30, 20))];
    huozanLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-100, 35, 30, 20))];
    zanGotLabel.font = [UIFont systemFontOfSize:14];
    zanGotLabel.textAlignment = NSTextAlignmentCenter;
    huozanLabel.textAlignment = NSTextAlignmentCenter;
    huozanLabel.font = [UIFont systemFontOfSize:12];
    huozanLabel.textColor = [UIColor grayColor];
    //获评
    pingGotLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-15, 15, 30, 20))];
    huopingLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2-15, 35, 30, 20))];
    huopingLabel.textAlignment = NSTextAlignmentCenter;
    pingGotLabel.font = [UIFont systemFontOfSize:14];
    pingGotLabel.textAlignment = NSTextAlignmentCenter;
    huopingLabel.font = [UIFont systemFontOfSize:12];
    huopingLabel.textColor = [UIColor grayColor];

    //发帖
    postedLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2+70, 15, 30, 20))];
    fatieLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2+70, 35, 30, 20))];
    postedLabel.font = [UIFont systemFontOfSize:14];
    postedLabel.textAlignment = NSTextAlignmentCenter;
    fatieLabel.textAlignment = NSTextAlignmentCenter;
    fatieLabel.font = [UIFont systemFontOfSize:12];
    fatieLabel.textColor = [UIColor grayColor];

    //zanGotLabel.text = @"22";
    huozanLabel.text = @"获赞";
    //pingGotLabel.text = @"33";
    huopingLabel.text = @"获评";
    //postedLabel.text = @"23";
    fatieLabel.text = @"发帖";

    [numView addSubview:zanGotLabel];
    [numView addSubview:huozanLabel];
    [numView addSubview:pingGotLabel];
    [numView addSubview:huopingLabel];
    [numView addSubview:postedLabel];
    [numView addSubview:fatieLabel];
    
    myTableView.tableHeaderView = tableHeaderView;
}

- (void)configTableView {
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,ScreenW,ScreenH-SafeAreaTopHeight) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.showsVerticalScrollIndicator = NO;
    [myTableView registerClass:[TalkTableCell class] forCellReuseIdentifier:@"TalkTableCell"];
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.rowHeight = UITableViewAutomaticDimension;
    myTableView.estimatedRowHeight = 450.0f;
    [self.view addSubview:myTableView];
    
    myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pageNum = 1;
        [self loadData];
        [dataArray removeAllObjects];
        [self loadTalkDataWithType:[NSString stringWithFormat:@"%ld",selectedBtn.tag-100]];
        
    }];
    myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadTalkDataWithType:[NSString stringWithFormat:@"%ld",selectedBtn.tag-100]];
    }];
}

- (void)configNaviItem {
    
    UIButton *publishButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [publishButton setImage:GetImage(@"编辑_new") forState:(UIControlStateNormal)];
    publishButton.titleLabel.textColor = [UIColor whiteColor];
    publishButton.frame = CGRectMake(0, 0, 40, 40);
    [publishButton addTarget:self action:@selector(gotoPublishButtonClicked) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *naviRightItem = [[UIBarButtonItem alloc] initWithCustomView:publishButton];
    self.navigationItem.rightBarButtonItem = naviRightItem;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
